<template>
  <div class="app-container">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="参数名" clearable />
      </el-form-item>
      <el-form-item>

        <el-button @click="getDataList()">查询</el-button>
        <el-button v-hasPermi="['wx:wxaccount:save']" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button v-hasPermi="['wx:wxaccount:delete']" type="danger" :disabled="dataListSelections.length <= 0" @click="deleteHandle()">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table :key="tableKey" v-loading="listLoading" :data="list" style="width: 100%;" @selection-change="selectionChangeHandle">
      <el-table-column type="selection" header-align="center" align="center" width="50" />
      <el-table-column prop="appid" header-align="center" align="center" label="appid" />
      <el-table-column prop="name" header-align="center" align="center" label="公众号名称" />
      <el-table-column prop="type" header-align="center" align="center" label="类型">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wx_account_type" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column prop="verified" header-align="center" align="center" label="是否认证">
        <span slot-scope="scope">{{ scope.row.verified?"是":"否" }}</span>
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="200" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="accessInfo(scope.row)">接入</el-button>
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.appid)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList" />
    <account-access v-if="accountAccessVisible" ref="accountAccessDialog" />
  </div>
</template>

<script>
import AddOrUpdate from './account/wx-account-add-or-update'
import AccountAccess from './account/wx-account-access-info'
import { mapState } from 'vuex'
import { tableInfo, crudUtil, sortUtil } from '@/utils/future/toolkit'
import { fetchWxAccountList, delWxAccount } from '@/api/wx'
export default {
  name: 'WxAccount',
  dicts: ['wx_account_type'],
  data() {
    const tableData = {
      dataForm: {
        key: ''
      },
      dataListSelections: [],
      addOrUpdateVisible: false,
      accountAccessVisible: false,
      showSearch: true, // 显示搜索条件
      queryParams: {
        scheduleName: undefined,
        createUid: undefined
      },
      promotionDate: []

    }
    return Object.assign(tableData, tableInfo)
  },
  components: {
    AddOrUpdate, AccountAccess
  },
  computed: mapState({
    ACCOUNT_TYPES: state => state.wxAccount.ACCOUNT_TYPES
  }),
  created() {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList() {
      var that = this
      const queryParams = {}
      that.queryParams = queryParams
      crudUtil.getList(that, fetchWxAccountList)
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle(item) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(item)
      })
    },
    accessInfo(item) {
      this.accountAccessVisible = true
      this.$nextTick(() => {
        this.$refs.accountAccessDialog.init(item)
      })
    },
    // 删除
    deleteHandle(appid) {
      var ids = appid ? [appid] : this.dataListSelections.map(item => {
        return item.appid
      })
      this.$confirm(`确定对[appid=${ids.join(',')}]进行[${appid ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delWxAccount(ids).then(response => {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.getDataList()
            }
          })
        })
      })
    }
  }
}
</script>
